<template>
    <div class="edu-page-warp">
        <condition-card @reset="reset" @search="onSubmit" label-width="120px">
            <el-col :span="8">
                <el-form-item :label="$t('label.nowGrade')">
                    <el-date-picker
                        v-model="queryForm.grade"
                        type="year"
                        value-format="yyyy"
                        style="width:100%"
                        :clearable="false"
                        @change="handleGradeChanged"
                    >
                    </el-date-picker>
                </el-form-item>
            </el-col>
             <el-col :span="8">
                <el-form-item label="结业年级">
                    <el-date-picker
                        v-model="queryForm.completionYear"
                        type="year"
                        value-format="yyyy"
                        :placeholder="$t('message.plzSelect')" style="width:100%"
                        >
                    </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('label.college')">
                    <el-select
                        v-model="queryForm.faculty"
                        style="width:100%"
                        filterable
                        @change="changeFaculty"
                        clearable
                    >
                        <el-option
                            v-for="(item, index) in options.faculty"
                            :key="index"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('label.major')">
                    <el-select
                        v-model="queryForm.profession"
                        style="width:100%"
                        filterable
                        clearable
                    >
                        <el-option
                            v-for="(item, index) in source.majors"
                            :key="index"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="志愿专业">
                    <el-select
                        v-model="queryForm.choice"
                        style="width:100%"
                        filterable
                        clearable
                    >
                        <el-option
                            v-for="(item, index) in options.major"
                            :key="index"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-col>
        </condition-card>
        <el-card shadow="never">
            <div class="edu-page-title">成绩管理</div>
            <el-row style="margin-bottom:15px">
                <el-col :span="4">
                    <el-input
                        v-model="queryForm.keyword"
                        :placeholder="$t('score.failScoreManage.pleaseInt')"
                        @keyup.enter.native="onSubmit"
                        style="width:200px"
                    >
                        <i
                            class="el-icon-search"
                            slot="suffix"
                            style="margin-top:10px;cursor:pointer"
                            @click="onSubmit"
                        ></i>
                    </el-input>
                </el-col>
                <el-col :span="20">
                  <div class="float-right">
                    <el-button @click="onExport">{{$t('message.export')}}</el-button>
                  </div>
                </el-col>
            </el-row>
            <edu-table
                id="score-list"
                ref="mainTable"
                :order="false"
                :selection="false"
                :pageNum="pageNum_"
                :pageSize="pageSize_"
                :total="total_"
                :data="data"
                :key="timestamp"
                @page-change="handlePageChanged"
            >
                <el-table-column :label="$t('label.stuCode')" width="80" prop="studentCode"  fixed="left"/>
                <el-table-column
                    :label="$t('label.stuName')"
                    width="150"
                    prop="studentName"
                    show-overflow-tooltip
                    fixed="left"
                />
                <el-table-column :label="$t('label.nowGrade')" width="100" prop="grade" fixed="left"/>
                <el-table-column label="结业年级" width="100" prop="completionYear" fixed="left"/>
                 <el-table-column label="预科生专业"  width="165" fixed="left" prop="className" show-overflow-tooltip />
                <el-table-column
                    :label="$t('label.college')"
                    show-overflow-tooltip
                    prop="facultyI18n"
                    width="120"
                />
                <el-table-column
                    :label="$t('label.major')"
                    show-overflow-tooltip
                    prop="professionI18n"
                    width="120"
                />
                <el-table-column label="志愿专业" width="180">
                    <template slot-scope="scope">
                        {{getMajorText(scope.row)}}
                    </template>
                </el-table-column>
                <el-table-column label="志愿序号" prop="level" width="100"/>
                <el-table-column label="第一学年平均绩点" width="140" prop="gradePoint" >
                    <template slot-scope="scope">
                        <el-tag size="medium" v-if="scope.row.gradePoint">{{ scope.row.gradePoint}}</el-tag>
                    </template>
                </el-table-column>
                <!--面试成绩-->
                <el-table-column label="专业选拔考核成绩"  width="140">
                        <template slot-scope="scope">
                            <el-input v-if="scope.row.isEdit"  v-model="scope.row.mScore" maxlength="8" />
                            <el-tag size="medium" v-else-if="scope.row.mScore" type="success">{{ scope.row.mScore}}</el-tag>
                        </template>
                    </el-table-column>
                <!--综合成绩-->
                <el-table-column label="综合成绩" width="100">
                    <template slot-scope="scope">
                        <el-tag size="medium" v-if="scope.row.zScore"  type="danger">{{ scope.row.zScore}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('label.operation')" width="100" fixed="right">
                    <template slot-scope="scope">
                        <div v-if="scope.row.gradePoint">
                            <el-button type="text" v-if="!scope.row.isEdit" @click="edit(scope.row)">
                                {{ $t('message.edit') }}
                            </el-button>
                            <div v-else>
                                <el-button type="text" @click="save(scope.row)">
                                    {{ $t('message.save') }}
                                </el-button>
                                <el-button type="text" @click="cancel(scope.row)">
                                    {{ $t('message.cancel') }}
                                </el-button>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import * as APIs from 'common/src/api/preparatoryGraduation/enroll'
import completionAudit from '../completionAudit.js'
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
    name: 'achievementManage',
    mixins: [completionAudit],
    data() {
        return {
            queryForm: {
                grade: null,
                completionYear:null,
                faculty: null,
                profession: null,
                keyword: null,
                choice:null
            },
            pageNum_: 1,
            pageSize_: 20,
            total_: 0,
            options: {
                faculty: [],
                major:[],
                lastMajor:[]
            },
            interviewResult: null,
            timestamp:null,
            number:[]
        }
    },
    mounted() {
        this.queryForm.grade=((new Date()).getFullYear()-1).toString();
        const data = {
            lang: this.$store.getters.language || 'cn',
            type: 'allChild',
            keys: ['X_YX']
        }
        queryDic(data).then(res => {
            if (res.code === 200) {
                this._fill('faculty', res.data.X_YX)
                this.options.faculty = this.options.faculty.filter(item => item.value.toString() === '000637')
            }
        })
        this.getMajorNumber();
        this.handleGradeChanged(this.queryForm.grade,true);
        this.onSubmit()
    },
    methods: {
        reset() {
            this.queryForm.grade=((new Date()).getFullYear()-1).toString();
            this.queryForm.completionYear=null;
            this.queryForm.faculty = null;
            this.queryForm.profession = null;
            this.queryForm.keyword = null;
            this.handleGradeChanged(this.queryForm.grade,true)
            this.source.majors=[];
            this.onSubmit();
        },
        _fill(type, data) {
            for (const key in data) {
                this.options[type].push({
                    value: key,
                    label: data[key]
                })
            }
        },
        changeFaculty() {
            this.queryForm.profession = null;
            this.queryForm.college = this.queryForm.faculty;
            this.initMajorData();
        },
        onSubmit() {
            this.options.lastMajor=_.cloneDeep(this.options.major);
            let param = {
                condition: _.cloneDeep(this.queryForm),
                pageSize_: this.pageSize_,
                pageNum_: this.pageNum_
            }
            APIs.getScoreListPage(param).then(res => {
                if (res.code == 200) {
                    this.timestamp=(new Date()).valueOf();
                    res.data.list.forEach(row => {
                        row.isEdit = false
                        let mScore=row.interviewResult?row.interviewResult.split(','):null//面试成绩
                        let zScore=row.finalGrade?row.finalGrade.split(','):null  //综合成绩
                        let level = row.level;

                        if(mScore&&level>0&&(level-1)<mScore.length){
                            row.mScore = mScore[level - 1];
                        }else{
                            row.mScore = null
                        }
                        if(zScore&&level>0&&(level-1)<zScore.length){
                            row.zScore = zScore[level - 1];
                        }else{
                            row.zScore =null
                        }
                        if(row.mScore == 0){
                            row.mScore = null;
                            row.zScore = null;
                        }
                    })
                    this.data = res.data.list
                    this.pageNum_ = res.data.pageNum_
                    this.pageSize_ = res.data.pageSize_
                    this.total_ = res.data.total_
                }
            })
        },
        edit(row) {
            row.isEdit = true
            row.mScoreOld=row.mScore;
        },
        save(row) {
            //验证每个专业考核成绩
            if(row.mScore.replace(/\s/g,"")==''||isNaN(row.mScore)){
                this.$message({
                    type:"warning",
                    message:'请输入正确格式'
                })
                return;
            }
            row.interviewResult=row.mScore.replace(/\s/g,"")
            APIs.setInterviewResult(row).then(res=>{
                if(res.code==200){
                    this.$message({
                        type:"success",
                        message:this.$t('message.saveSuccess')
                    })
                    row.isEdit = false;
                    this.onSubmit();
                }
            });
        },
        cancel(row) {
            row.isEdit = false
            row.mScore=row.mScoreOld
        },
        handlePageChanged(obj){
            this.pageNum_=obj.pageNum;
            this.pageSize_=obj.pageSize;
            this.onSubmit();
        },
        //获取当前专业设置数量
        getMajorNumber(){
            APIs.getAuthority().then(res=>{
                if(res.code==200){
                    if(res.data){
                        let number=res.data.choiceNumber;
                        if(number>10){
                            number=10;
                        }
                        for(let i=1;i<=number;i++){
                            let name=this.convertNum(i)
                            this.number.push({
                                label:`${name}专业选拔考核成绩`,
                                scoreLabel:`${name}综合成绩`,
                            })
                        }
                    }
                }
            });
        },
        convertNum(n){
            if(n<=0||n>10){
                throw "valid error"
            }
            let arr=['','一','二','三','四','五','六','七','八','九','十']
            return '志愿'+arr[n]
        },
        handleGradeChanged(val,isFirst){
            this.queryForm.choice = null;
            this.changeFaculty();
            APIs.getListPageURL2({condition:{grade:Number(val)+1},pageNum_:1,pageSize_:100}).then(res=>{
                if(res.code==200){
                    //去重复项
                    let codes=new Set();
                    res.data.list.forEach(p=>{
                        if(!codes.has(p.profession)){
                            codes.add(p.profession);
                            p.isValid=true;
                        }
                    });
                    this.options.major=res.data.list.filter(p=>p.isValid).map(m=>{
                        return {
                            label:m.professionName+"("+m.profession+")",
                            value:m.profession
                        }
                    })
                    if(isFirst)
                    this.options.lastMajor=_.cloneDeep(this.options.major);
                }
            });
        },
        getMajorText(row){
            let choice = this.options.lastMajor.find(m=>m.value == row.choice)
            return choice?choice.label:null
        },
      // 导出
      onExport() {
        let data ={};
        data.grade = this.queryForm.grade;
        APIs.exportachievementManage(data).then(res => {
          if (res.code == 200) {
            tools.downloadFile(res.data.key, '成绩管理.xls', null, this);
          }
        })
      },
    }
}
</script>
<style lang="scss" scoped>
#score-list {
    ::v-deep .el-tag--medium {
        font-size:16px;
    }
}
</style>
